﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ObjectManager;

namespace QuanLyDiem
{
    public partial class frmHocBong : Form
    {
        public static frmHocBong frm = null;
        private List<string> hocky = new List<string>();
        private List<string> namhoc = new List<string>();

        public frmHocBong()
        {
            InitializeComponent();
        }

        public static frmHocBong Create()
        {
            return Create(false);
        }

        public static frmHocBong Create(bool child)
        {
            if (frm == null)
            {
                frm = new frmHocBong();
            }
            if (child)
            {
                frm.ControlBox = false;
                frm.MinimizeBox = false;
                frm.MaximizeBox = false;
                frm.WindowState = FormWindowState.Maximized;
            }
            frm.LoadData();
            return frm;
        }

        private void HocBong_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (frm != null)
            {
                frm.Dispose();
                frm = null;
            }
        }

        private void LoadData()
        {
            dgvHocBong.DataSource = (new HocBong()).ListAll();
            namhoc.Add("Chọn");
            hocky.Add("Chọn");
            for (int i = 0; i < 8; i++)
            {
                if (i < 3)
                {
                    hocky.Add((i + 1).ToString());
                }
                namhoc.Add((i + 1).ToString());
            }
            cmbHocKy.DataSource = hocky;
            cmbNamHoc.DataSource = namhoc;
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            if (cmbHocKy.SelectedIndex != 0 && cmbNamHoc.SelectedIndex != 0)
            {
                frmReportHocBong frm = new frmReportHocBong((new HocBong()).ListAll(cmbNamHoc.Text, cmbHocKy.Text));
                frm.ShowDialog();
            }
            else
            {
                MessageBox.Show("Bạn phải chọn năm học và học kỳ muốn in danh sách", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (cmbNamHoc.SelectedIndex == 0)
            {
                dgvHocBong.DataSource = (new HocBong()).ListAll("%", "%");
            }
            else
            {
                if (cmbHocKy.SelectedIndex == 0)
                {
                    dgvHocBong.DataSource = (new HocBong()).ListAll(cmbNamHoc.Text, "%");
                }
                else
                {
                    dgvHocBong.DataSource = (new HocBong()).ListAll(cmbNamHoc.Text, cmbHocKy.Text);
                }
            }
        }

        private void cmbNamHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNamHoc.SelectedIndex == 0)
            {
                cmbHocKy.SelectedIndex = 0;
                dgvHocBong.DataSource = (new HocBong()).ListAll("%", "%");
            }
            else
            {
                if (cmbHocKy.SelectedIndex == 0)
                {
                    dgvHocBong.DataSource = (new HocBong()).ListAll(cmbNamHoc.Text, "%");
                }
                else
                {
                    dgvHocBong.DataSource = (new HocBong()).ListAll(cmbNamHoc.Text, cmbHocKy.Text);
                }
            }
        }

        private void cmbHocKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbHocKy.SelectedIndex != 0)
            {
                cmbNamHoc.SelectedIndex = 1;
                dgvHocBong.DataSource = (new HocBong()).ListAll(cmbNamHoc.Text, cmbHocKy.Text);
            }
            else
            {
                if (cmbNamHoc.SelectedIndex != 0)
                {
                    dgvHocBong.DataSource = (new HocBong()).ListAll(cmbNamHoc.Text, "%");
                }
                else
                {
                    dgvHocBong.DataSource = (new HocBong()).ListAll("%", "%");
                }
            }
        }
    }
}
